package com.example.messagedemo.config;

import com.alibaba.fastjson.JSONObject;
import com.example.messagedemo.model.base.BaseMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

/**
 * 消息redis监听器
 *
 * @author dlp
 * @date 2024/08/22
 */
@Slf4j
public abstract class MessageRedisListener  implements  MessageListener {

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String channel = new String(message.getChannel());
        String body = new String(message.getBody());
        log.info("接收到来自:" + channel + "频道的消息体:" + body);
        if (!body.isEmpty()){
            BaseMap map = JSONObject.parseObject(body, BaseMap.class);
            onMessage(map);
        }else{
            log.error("接收到来自:" + channel + "频道的没用的消息体:" + body);
        }
    }

    public abstract void onMessage(BaseMap baseMap);
}
